package cn.gok;

import java.io.*;
import java.lang.annotation.Annotation;
import java.util.Comparator;
import java.util.TreeSet;

class Student1 implements Comparable<Student1> {
    private int sid;
    private String name;
    private int age;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student1(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    @Override
    public int compareTo(Student1 o) {
        if (this.age<o.age) {
            return -1;
        }
        return 1;
    }
}

class StudentName implements Comparator<Student1> {

    @Override
    public int compare(Student1 o1, Student1 o2) {
        if (o1.getName().length()<o2.getName().length()) {
            return -1;
        }
        return 1;
    }
}
class School implements Serializable{
    private String name;
    private String address;
    private int birthDay;

    public School(String name, String address, int birthDay) {
        this.name = name;
        this.address = address;
        this.birthDay = birthDay;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthDay=" + birthDay +
                '}';
    }

}
public class homework５ {
    public static void main(String[] args)throws IOException,ClassNotFoundException {
        //1.文件流对象
        //写入数据();
        //readFromFile();
        //2.自定义规则
        //按年龄排序();
        按名字长短排序();


    }

    private static void 按名字长短排序() {
        TreeSet<Student1> set=new TreeSet<>(new StudentName());
        set.add(new Student1(111,"小小明",22));
        set.add(new Student1(112,"小明",23));
        set.stream().forEach(System.out::println);
    }

    private static void 按年龄排序() {
        TreeSet<Student1> set=new TreeSet<>();
        set.add(new Student1(111,"小明",22));
        set.add(new Student1(112,"小张",23));
        set.stream().forEach(System.out::println);
    }

    private static void readFromFile() throws IOException,ClassNotFoundException{
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day5//school.dat"));
        try {while (true){
            Object obj=ois.readObject();
            System.out.println(obj);}
        }catch (EOFException e){//通过捕获文件末尾异常，跳出死循环
            System.out.println("文件读取结束!");
        }
        ois.close();

    }

    private static void 写入数据() throws IOException{
        ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("day5//school.dat"));
        School school1=new School("北京大学","北京",2002);
        School school2=new School("厦门大学","厦门",2010);
        oos.writeObject(school1);
        oos.writeObject(school2);
        System.out.println("写入成功!");
        oos.close();
    }
}
